package com.lupan.springboot;

import com.lupan.springboot.bean.MyBean;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.*;

/**
 * 说明：
 *
 * @author lupan
 * @version 2017/6/13 14:20
 */

@RestController
@EnableAutoConfiguration
@ComponentScan("com.lupan.springboot")
public class Example {
    @RequestMapping("example")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        //传统方式启动
//      SpringApplication.run(Example.class, args);

        //通过builder启动
        ApplicationContext context = new SpringApplicationBuilder()
                .sources(Example.class)
                .listeners(new ApplicationListener<ApplicationReadyEvent>(){

                    @Override
                    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
                        ApplicationContext context = applicationReadyEvent.getApplicationContext();
                        System.out.println("spring启动完毕。。。。");
                    }
                })
                .run(args);


        MyBean myBean = context.getBean("myBean",MyBean.class);
        System.out.println("name="+myBean.getName());
        System.out.println(myBean.getNames());
    }

}
